package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.service.SysDeptService;
import com.ailanyin.common.model.SysDept;
import com.ailanyin.common.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 上午 10:18
 */
@RestController
@RequestMapping("/system/dept")
@Api(tags = "后台部门管理")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @PostMapping
    @ApiOperation(value = "添加部门")
    @PreAuthorize("hasAuthority('system:dept:add')")
    public AjaxResult addDept(@Validated @RequestBody SysDept dept) {
        sysDeptService.addDept(dept);
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation(value = "修改部门")
    @PreAuthorize("hasAuthority('system:dept:edit')")
    public AjaxResult updateDept(@Validated @RequestBody SysDept dept) {
        sysDeptService.updateDept(dept);
        return AjaxResult.success();
    }

    @DeleteMapping("/{deptId}")
    @ApiOperation(value = "删除部门")
    @PreAuthorize("hasAuthority('system:dept:remove')")
    public AjaxResult removeDept(@PathVariable Long deptId) {
        sysDeptService.removeDept(deptId);
        return AjaxResult.success();
    }

    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    @ApiOperation(value = "角色部门列表树")
    public AjaxResult roleDeptTreeSelect(@PathVariable("roleId") Long roleId) {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        AjaxResult result = AjaxResult.success();
        result.put("checkedKeys", sysDeptService.selectDeptListByRoleId(roleId));
        result.put("depts", sysDeptService.buildDeptTreeSelect(deptList));
        return result;
    }

    @GetMapping("/treeselect")
    @ApiOperation(value = "部门下拉列表树")
    public AjaxResult treeSelect(SysDept dept) {
        List<SysDept> deptList = sysDeptService.selectDeptList(dept);
        return AjaxResult.success(sysDeptService.buildDeptTreeSelect(deptList));
    }

    @GetMapping(value = "/{deptId}")
    @ApiOperation(value = "部门详细信息")
    @PreAuthorize("hasAuthority('system:dept:query')")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        sysDeptService.checkDeptDataScope(deptId);
        return AjaxResult.success(sysDeptService.selectDeptById(deptId));
    }

    @GetMapping("/list/exclude/{deptId}")
    @ApiOperation(value = "查询部门列表(排除节点)")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        Iterator<SysDept> it = deptList.iterator();
        while (it.hasNext()) {
            SysDept d = (SysDept) it.next();
            if (d.getDeptId().intValue() == deptId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(deptList);
    }

    @GetMapping("/list")
    @ApiOperation(value = "部门列表")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public AjaxResult list(SysDept dept) {
        return AjaxResult.success(sysDeptService.selectDeptList(dept));
    }
}
